<?php
namespace app\index\controller;

use think\facade\Request;
use think\Controller;
use think\Db;
class Renwu extends Controller
{
    public function index()
    {
        $str=Db::table('renwutype')->select();
        $res=Db::table('renwu')->alias('ren')->join('renwutype type','ren.name1=type.uid')->select();
         return $this->fetch('renwu',['res'=>$res,'str'=>$str]);
    }
    public function shows(){
        $type=input('get.type');
        // echo $type;
        if($type==1){
            $res=Db::table('renwu')->alias('ren')->join('renwutype type','ren.name1=type.uid')->order('money desc')->select();
            return json_encode(['res'=>$res]);
        }else if($type==2){
            $res=Db::table('renwu')->alias('ren')->join('renwutype type','ren.name1=type.uid')->order('times asc')->select();
            return json_encode(['res'=>$res]);
        }else if($type==3){
            $res=Db::table('renwu')->alias('ren')->join('renwutype type','ren.name1=type.uid')->order('money desc')->select();
            return json_encode(['res'=>$res]);
        }else if($type==4){
            $res=Db::table('renwu')->alias('ren')->join('renwutype type','ren.name1=type.uid')->order('money asc')->select();
            return json_encode(['res'=>$res]);
        }
    }
        
    public function renwu(){
        $id=input('get.id');
        if($id==0){
            $res=Db::table('renwu')->alias('ren')->join('renwutype type','ren.name1=type.uid')->select();
            return json_encode(['res'=>$res]);die;
        }else{
             $res=Db::table('renwu')->alias('ren')->join('renwutype type','ren.name1=type.uid')->where('ren.name1',$id)->select();
            return json_encode(['res'=>$res]);die;
        }
    }
    public function show(){
    	$id=input('get.id');
    	// $res=Db::table('renwu')->where("id=$id")->select();
    	return json_encode(['id'=>$id]);
    	// return $this->fetch('',['res'=>$res]);
    }
    public function bounty(){
    	$id=input('get.id');
    	$res=Db::table('renwu')->where("id=$id")->select();
    	// var_dump($res);die;
    	// return $this->fetch('bounty');
    	return $this->fetch('bounty',['res'=>$res]);
    }
    public function announcement(){
    	return $this->fetch('announcement');
    }
    public function reward(){
        $res=Db::table('renwutype')->select();
        // var_dump($res);
    	return $this->fetch('reward',['res'=>$res]);
    }
    public function type(){
    	$type=input('get.type');
    	// $res=Db::table('renwu')->where("id=$id")->select();
    	return json_encode(['type'=>$type]);
    }
    public function type1(){
    	$type=input('get.type');
        $res=Db::table('renwutype')->where('uid',$type)->find();
        return $this->fetch('type',['res'=>$res]);
    }
    public function adds(){
    	$id=input('post.id');
        $str=Db::table('renwutype')->where("uid",$id)->find();
        $type=$str['type'];
            // var_dump($str);die;
        // echo db::name('renwutype')->getLastSql($str);die;
    	$pro_name=input('post.pro_name');
    	$title=input('post.title');
    	$unit_price=input('post.unit_price');
    	$ticket_num=input('post.ticket_num');
    	$submittime=input('post.submittime');
    	$periodtime=input('post.periodtime');
    	$chongzuo=input('post.chongzuo');
    	$category_id=input('post.category_id');
    	$count=input('post.count');
    	$data=['pro_name'=>$pro_name,'title'=>$title,'unit_price'=>$unit_price,'ticket_num'=>$ticket_num,'submittime'=>$submittime,'periodtime'=>$periodtime,'chongzuo'=>$chongzuo,'category_id'=>$category_id,'count'=>$count,'rwtype'=>$id];
    	$res=Db::table('adds')->insert($data);
        
    	// $data=['下载注册','网页注册','认证绑卡','高价任务','投票浏览','关注转发','数据采集','其他项目'];
    	// // var_dump($data);
    	if($res){
            
    		return json_encode(['code'=>0,'msg'=>'基本信息设置成功','type'=>$type]);
    	}else{
    		return json_encode(['code'=>1,'msg'=>'基本信息设置失败']);
    	}
	}
	public function release(){
		$type=input('get.type');
		$pro_name=input('get.pro_name');
		// echo $biaoti;
		$res=Db::table('adds')->where("pro_name='$pro_name'")->find();
		return $this->fetch('release',['type'=>$type,'pro_name'=>$pro_name,'res'=>$res]);
	}
	public function preview(){
		$pro_name=input('get.pro_name');
		$res=Db::table('adds')->where("pro_name='$pro_name'")->find();
    	// var_dump($res);
		return $this->fetch('preview',['res'=>$res]);
	}
	public function addtook(){
		$id=input('post.id');
		// echo "$id";die;
		$res=Db::table('adds')->where("id",$id)->update(['status' => '1']);
		// $res=Db::table('adds')->where("id='$id'")->find();
	 //    // var_dump($res);die;
		// $time=date('Y-m-d');
		// $data=['title'=>$res['pro-name'],'money'=>$res['unit_price'],'count'=>0,'times'=>$time,'remaining'=>$res['count'],'img'=>'http://www.1612agroup1.com/public/image/user.png'];
  //   	$res=Db::table('renwu')->insert($data);
    	if($res){ 
    		return json_encode(['code'=>0,'msg'=>'发布成功，请等待审核','id'=>$id]);
    	}else{
    		return json_encode(['code'=>1,'msg'=>'发布的任务已存在']);
    	}
	}
	public function addss(){
		$id=input('get.id');
		// $res=Db::table('adds')->where("id='$id'")->find();
		$res=Db::table('adds')->select();
		return $this->fetch('addtook',['res'=>$res]);
	}
	public function cancel(){
		$id=input('get.id');
		$str=Db::table('adds')->where("id='$id'")->find();
		if($str['status']==1){
			$res=Db::table('adds')->where("id",$id)->update(['status' => '0']);
			return json_encode(['code'=>0,'msg'=>'已下架']);die;
		}else{
			$res=Db::table('adds')->where("id",$id)->update(['status' => '1']);
			return json_encode(['code'=>1,'msg'=>'已发布']);die;
		}
	}
    public function suspended(){
        $id=input('get.id');
        $str=Db::table('adds')->where("id='$id'")->find();
        if($str['suspended']==1){
            $res=Db::table('adds')->where("id",$id)->update(['suspended' => '0']);
            return json_encode(['code'=>0,'msg'=>'开启任务']);die;
        }else{
            $res=Db::table('adds')->where("id",$id)->update(['suspended' => '1']);
            return json_encode(['code'=>1,'msg'=>'暂停任务']);die;
        }
    }
	public function management(){
		$id=input('get.id');
		return json_encode(['code'=>0,'id'=>$id]);
	}
	public function management1(){
		$id=input('get.id');
		$str=Db::table('adds')->where("id='$id'")->find();
		return $this->fetch('management1',['id'=>$id,'res'=>$str]);
	}
	public function update(){
		$id=input('get.id');
		$str=Db::table('adds')->where("id='$id'")->find();
		return $this->fetch('update',['res'=>$str]);
	}
    public function update1(){
        $id=input('get.id');
        $title=input('post.title');
        $pro_name=input('post.pro_name');
        $submittime=input('post.submittime');
        $periodtime=input('post.periodtime');
        $chongzuo=input('post.chongzuo');
        $res=Db::table('adds')->where("id",$id)->update(['pro_name'=>$pro_name,'title'=>$title,'submittime'=>$submittime,'periodtime'=>$periodtime,'chongzuo'=>$chongzuo]);
        if($res){
            return json_encode(['code'=>0,'msg'=>'保存成功']);die;
        }else{
            return json_encode(['code'=>1,'msg'=>'保存失败']);die;
        }
    }
    public function amount(){
       $id=input('get.id');
       $hour=input('get.hour');
       $res=Db::table('adds')->where("id",$id)->find();
       $str=$res['ticket_num']+$hour;
       $res=Db::table('adds')->where("id",$id)->update(['ticket_num'=>$str]);
       if($res){
            return json_encode(['code'=>0,'msg'=>'添加成功']);
        }else{
            return json_encode(['code'=>1,'msg'=>'添加失败']);
        }
    }
    public function jine(){
       $id=input('get.id');
       $hour=input('get.hour');
       $res=Db::table('adds')->where("id",$id)->find();
       $str=$res['unit_price']+$hour;
       $res=Db::table('adds')->where("id",$id)->update(['unit_price'=>$str]);
       if($res){
            return json_encode(['code'=>0,'msg'=>'添加成功']);
        }else{
            return json_encode(['code'=>1,'msg'=>'添加失败']);
        }
    }

    public function classification(){
        $val=input('get.val');
        if($val=='全部'){
            $res=Db::table('adds')->select();
            return json_encode(['res'=>$res]);die;
        }else if($val=='待审核'){
            $res=Db::table('adds')->where('audit=0')->select();
            return json_encode(['res'=>$res]);die;
        }else if($val=='进行中'){
            $res=Db::table('adds')->where('audit=1')->select();
            return json_encode(['res'=>$res]);die;
        }else if($val=='已暂停'){
            $res=Db::table('adds')->where('suspended=1')->select();
            return json_encode(['res'=>$res]);die;
        }else if($val=='完成'){
            $res=Db::table('adds')->where('ticket_num=0')->select();
            return json_encode(['res'=>$res]);die;
        }
    }
    public function addto(){
        // return 1111;
        // $formData[]=input('post.');

        $formData = $this->request->param('');
        var_dump($formData);die;
    }
}